<template>
  <el-dialog
    :title="!dataForm.id ? '新建' : isDetail ? '详情' : '编辑'"
    :close-on-click-modal="false"
    append-to-body
    :visible.sync="visible"
    class="jsbos-dialog jsbos-dialog_center"
    lock-scroll
    width="800px"
  >
    <el-row :gutter="15" class="">
      <el-form
        v-loading="loading"
        ref="elForm"
        :model="dataForm"
        :rules="rules"
        size="small"
        label-width="100px"
        label-position="right"
      >
        <template v-if="!loading">
          <el-col :span="12">
            <el-form-item label="培训名称" prop="name">
              <el-input
                v-model="dataForm.name"
                placeholder="请输入培训名称"
                :maxlength="50"
                clearable
                :style="{ width: '100%' }"
              >
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="培训预算" prop="budget">
              <Amount
                v-model="dataForm.budget"
                placeholder="金额文本"
                :step="1"
                controls-position="right"
              >
              </Amount>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="培训地点" prop="address">
              <el-input
                v-model="dataForm.address"
                placeholder="请输入培训名称"
                :maxlength="50"
                clearable
                :style="{ width: '100%' }"
              >
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="开始时间" prop="startDatetime">
              <el-date-picker
                v-model="dataForm.startDatetime"
                placeholder="请选择"
                clearable
                :style="{ width: '100%' }"
                type="date"
                format="yyyy-MM-dd"
                value-format="timestamp"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="截至时间" prop="endDatetime">
              <el-date-picker
                v-model="dataForm.endDatetime"
                placeholder="请选择"
                clearable
                :style="{ width: '100%' }"
                type="date"
                format="yyyy-MM-dd"
                value-format="timestamp"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="讲师" prop="lecturerUserId">
              <user-select
                v-model="dataForm.lecturerUserId"
                placeholder="请选择"
                clearable
              >
              </user-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="负责人" prop="curator">
              <user-select
                v-model="dataForm.curator"
                placeholder="请选择"
                clearable
              >
              </user-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="面授课程" prop="courseFaceId">
              <custom-select
                v-model="dataForm.courseFaceId"
                placeholder="请选择"
                clearable
              >
              </custom-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="经费来源" prop="sourceFunding">
              <el-select
                v-model="dataForm.sourceFunding"
                placeholder="请选择"
                clearable
                :style="{ width: '100%' }"
              >
                <el-option
                  v-for="(item, index) in sourceFundingOptions"
                  :key="index"
                  :label="item.fullName"
                  :value="item.id"
                  :disabled="item.disabled"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="课时" prop="classHour">
              <el-input-number
                v-model="dataForm.classHour"
                placeholder="数字文本"
                :max="100"
                :step="1"
                :precision="1"
                controls-position="right"
              >
              </el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="描述" prop="description">
              <el-input
                v-model="dataForm.description"
                placeholder="请输入"
                :maxlength="200"
                :style="{ width: '100%' }"
                true
                type="textarea"
                :autosize="{ minRows: 4, maxRows: 4 }"
              >
              </el-input>
            </el-form-item>
          </el-col>
        </template>
      </el-form>
    </el-row>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false"> 取 消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" v-if="!isDetail">
        确 定</el-button
      >
    </span>
  </el-dialog>
</template>
<script>
import request from "@/utils/request";
import { getDataInterfaceRes } from "@/api/systemData/dataInterface";

export default {
  components: {},
  props: [],
  data() {
    return {
      visible: false,
      loading: false,
      isDetail: false,
      dataForm: {
        planId: "",
        name: "",
        budget: "",
        address: "",
        startDatetime: "",
        endDatetime: "",
        lecturerUserId: "",
        curator: "",
        courseFaceId: "",
        sourceFunding: "",
        classHour: 0,
        description: "",
        creatorUserId: "",
        creatorTime: "",
        lastModifyUserId: "",
        lastModifyTime: "",
      },
      rules: {
        name: [
          {
            required: true,
            message: "请输入培训名称",
            trigger: "blur",
          },
        ],
      },
      sourceFundingOptions: [
        { fullName: "教育经费", id: "1" },
        { fullName: "行政经费", id: "2" },
      ],
    };
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    clearData(data) {
      for (let key in data) {
        if (data[key] instanceof Array) {
          data[key] = [];
        } else if (data[key] instanceof Object) {
          this.clearData(data[key]);
        } else {
          data[key] = "";
        }
      }
    },
    init(id, isDetail) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.isDetail = isDetail || false;
      this.$nextTick(() => {
        this.$refs["elForm"].resetFields();
        if (this.dataForm.id) {
          this.loading = true;
          request({
            url: "/api/course/TemsPlanDetail/" + this.dataForm.id,
            method: "get",
          }).then((res) => {
            this.dataInfo(res.data);
            this.loading = false;
          });
        } else {
          this.clearData(this.dataForm);
        }
      });
      this.$store.commit("generator/UPDATE_RELATION_DATA", {});
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["elForm"].validate((valid) => {
        if (valid) {
          this.request();
        }
      });
    },
    request() {
      var _data = this.dataList();
      this.visible = false;
      this.$emit("refresh", _data);
    },
    dataList() {
      var _data = JSON.parse(JSON.stringify(this.dataForm));
      return _data;
    },
    dataInfo(dataAll) {
      let _dataAll = dataAll;
      this.dataForm = _dataAll;
    },
  },
};
</script>
